<template>   
  <div >


    <div class="bo">
    <div class="container">

      <h1 id="he">Please Login</h1>
      
        <div class="form-control">
          <input type="text" required v-model="username">
          <label>
            <span style="transition-delay: 0ms">请</span>
              <span style="transition-delay: 50ms">输</span>
              <span style="transition-delay: 180ms">入</span>
            <span style="transition-delay: 150ms">用</span>
              <span style="transition-delay: 20ms">户</span>
              <span style="transition-delay: 100ms">名</span>        
        </label> 
        </div>

        <div class="form-control">
          <input type="password" required v-model="password">
          <label>
            <span style="transition-delay: 0ms">请</span>
              <span style="transition-delay: 50ms">输</span>
              <span style="transition-delay: 180ms">入</span>
            <span style="transition-delay: 150ms">密</span>
              <span style="transition-delay: 20ms">码</span>
          </label>
        </div>

        <button class="btn" @click="check()" >Login</button>

        <p class="text">Don't have an account? <a id="a" href="/#/toRegist">Register</a> </p>
      
    </div>
    </div>



  </div>
</template>


<script>

  export default {
      data () {
          return {
            username:'',
            password:'',
          }
      },
      methods: {
        
        check(){
         
          if(this.username=='' || this.password==''){
            this.$message({
                                type: 'error',
                                message: '用户名和密码不能为空！'
                            });
          }else{
            this.toLog()
          }

           
        },

        toLog(){
          this.axios({
            method: 'post',
            url: '/manage/token',
            data: {
              userName: this.username,
              userPassword: this.password
            }
          })
          .then(res=>{                    //请求成功后执行函数
            if(res.data.error_massage === "success"){
              this.VAR.userName=this.username
              this.VAR.password=this.password
              this.VAR.token=res.data.token
              this.VAR.userid=res.data.userid
              //利用localstorage存储到本地
              localStorage.setItem("token",this.VAR.token)
              localStorage.setItem("userName",this.VAR.userName)
              localStorage.setItem("password",this.VAR.password)
              localStorage.setItem("userid",this.VAR.userid)
              this.$router.push({name: 'homePage'}) 	//登录验证成功路由实现跳转
              this.$message({
                                type: 'success',
                                message: '登录成功!'
                            });
            }else{        
              console.log("登录失败")
            }
            console.log(res)
          })
          .catch(err=>{                   //请求错误后执行函
            this.$message({
                                type: 'error',
                                message: '用户名不存在或密码错误!'
                            });
            //console.log("请求错误")
          })  
        }

      },

      beforeCreate () {
          document.querySelector('body').setAttribute('style', 'background:#ffffff;')
          
         },

      beforeDestroy () {
        document.querySelector('body').removeAttribute('style')
      }
  }
</script>
<style scoped>
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');

* {
  box-sizing: border-box;
}

.bo {
  background-image:url('../assets/bg.jpg');
  background-size: 100%;
  color: #fff;
  font-family: 'Muli', sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}

.container {
  background-color: rgba(0, 0, 0, 0.4);
  padding: 20px 40px;
  border-radius: 5px;
}

.container h1 {
  text-align: center;
  margin-bottom: 30px;
}

.container a {
  text-decoration: none;
  color: lightblue;
}

.btn {
  cursor: pointer;
  display: inline-block;
  width: 100%;
  background: lightblue;
  padding: 15px;
  font-family: inherit;
  font-size: 16px;
  border: 0;
  border-radius: 5px;
}

.btn:hover {
  outline: 0;
  transform: scale(1.02);
}

.btn:active {
  transform: scale(0.98);
}

.text {
  margin-top: 30px;
}

#a:hover {
  transform: scale(1.05);
}

.form-control {
  position: relative;
  margin: 20px 0 40px;
  width: 300px;
}

.form-control input {
  background-color: transparent;
  border: 0;
  border-bottom: 2px #fff solid;
  display: block;
  width: 100%;
  padding: 15px 0;
  font-size: 18px;
  color: #fff;
}

.form-control input:focus,
.form-control input:valid {
  outline: 0;
  border-bottom-color: lightblue;
}

.form-control label {
  position: absolute;
  top: 15px;
  left: 0;
  pointer-events: none;
}

.form-control label span {
  display: inline-block;
  font-size: 18px;
  min-width: 5px;
  transition: 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.form-control input:focus + label span,
.form-control input:valid + label span {
  color: lightblue;
  transform: translateY(-30px);
}

  
</style>